package com.jinlejia.erp.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.databinding.ItemSellingLayoutBinding;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.Map;

/**
 * 出售Adapter
 */
public class SellingAdapter extends YfListAdapter<Map<String, Object>> {


    private Context context;
    private String showBtn;

    public SellingAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    public SellingAdapter(ArrayList<Map<String, Object>> data, String showbtn) {
        super(data);
        this.showBtn = showbtn;
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selling_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        ItemSellingLayoutBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);

        final Map<String, Object> map = mData.get(i);
        if (map.containsKey("image"))
            Glide.with(context).load(ApiConfig.MAGEHEADER_URL() + map.get("image")).placeholder(R.mipmap.ic_no_img).error(R.mipmap.ic_no_img).into(binding.ivImage);
        if (map.containsKey("imageUrl"))
            Glide.with(context).load(ApiConfig.MAGEHEADER_URL() + map.get("imageUrl")).placeholder(R.mipmap.ic_no_img).error(R.mipmap.ic_no_img).into(binding.ivImage);
        //小区名
        if (map.containsKey("estate"))
            binding.tvHouseName.setText(map.get("estate").toString());
        //商圈
        if (map.containsKey("county"))
            binding.tvHouseDistrict.setText(map.get("county").toString() + " - " + map.get("area").toString());
        //户型
        if (map.containsKey("houseType")) {
            binding.tvHouseType.setText(map.get("houseType").toString());
            if (TextUtils.isEmpty(mData.get(i).get("houseType").toString())) {
                binding.tvHouseType.setVisibility(View.GONE);
            } else {
                binding.tvHouseType.setVisibility(View.VISIBLE);
            }
        }
        //责字显示判断
        if (map.containsKey("isDuty")){
            if (Integer.parseInt(String.valueOf(map.get("isDuty")))==0){
                binding.tvIsDuty.setVisibility(View.GONE);
            }else{
                binding.tvIsDuty.setVisibility(View.VISIBLE);
            }
        }
        //面积
        if (map.containsKey("square"))
            binding.tvHouseArea.setText(map.get("square") + "㎡");
        //均价
        if (map.containsKey("unitPrice")) {
            binding.tvHouseAveragePrice.setText(map.get("unitPrice") + "元/㎡");
        } else {
            java.text.DecimalFormat myformat = new java.text.DecimalFormat("0.00");
            if (map.containsKey("price") && !TextUtils.isEmpty("price") && map.containsKey("square") && TextUtils.isEmpty("square")) {
                double floor = Double.parseDouble(map.get("price").toString()) * 10000 / Double.parseDouble(map.get("square").toString());
                binding.tvHouseAveragePrice.setText(myformat.format(floor) + "元/㎡");
            }
        }
        if (map.containsKey("price"))
            binding.tvHouseTotle.setText(map.get("price") + "万元");
        //售价
        if (map.containsKey("salePrice"))
            binding.tvHouseTotle.setText(map.get("salePrice") + "万元");


        if (!TextUtils.isEmpty(showBtn) && showBtn.equals("DEL")) {
            binding.btDelete.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(showBtn) && showBtn.equals("CHECK")) {
            binding.checkbox.setVisibility(View.VISIBLE);
        }

        //房源状态
        if (map.containsKey("state")) {
            switch (Integer.parseInt((String) map.get("state"))) {

                case 0:
                    binding.tvItemHouseStatus.setText("待发布");
                    break;

                case 1:
                    binding.tvItemHouseStatus.setText("待确认");
                    break;

                case 2:
                    binding.tvItemHouseStatus.setText("有效");
                    break;
                case 6:
                    binding.tvItemHouseStatus.setText("驳回");
                    break;
                case 11:
                    binding.tvItemHouseStatus.setText("驳回(经管)");
                    break;
                case 12:
                    binding.tvItemHouseStatus.setText("未知");
                    break;
                default:
                    binding.tvItemHouseStatus.setText("暂无");
                    break;
            }
        }

        //房源掉状态
        if (map.containsKey("changeStateDays")&&!map.get("changeStateDays").equals(null)) {
            binding.tvState.setText((String) map.get("changeStateDays"));
            Integer isRed = (Integer) map.get("isRed");
            if (isRed == 1) {
                binding.tvState.setTextColor(Color.parseColor("#FF0000"));
            } else {
                binding.tvState.setTextColor(Color.parseColor("#108EE9"));
            }
        }else{
            binding.tvState.setText("");
        }

        if (null != deleteListener) {
            binding.btDelete.setOnClickListener(v -> {
                //解决编码键值大小写不同
                if (map.containsKey("rsCode")) {
                    deleteListener.onDelete(i, map.get("rsCode").toString(), map.get("estate").toString());
                } else if (map.containsKey("rscode")) {
                    deleteListener.onDelete(i, map.get("rscode").toString(), map.get("estate").toString());
                }
            });
        }

        viewHolder.itemView.setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
            progress = itemView.findViewById(R.id.progress);
        }
    }

    static final class ViewHolder extends RecyclerView.ViewHolder {


        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    public interface OnDeleteListener {
        void onDelete(int position, String rscode, String name);
    }

    private OnDeleteListener deleteListener;

    public void setOnDeleteListener(OnDeleteListener listener) {
        this.deleteListener = listener;
    }

}
